package com.lanzhou.yuanfen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanzhou.yuanfen.config.UploadProperties;
import com.lanzhou.yuanfen.response.ServerResponsePage;
import com.lanzhou.yuanfen.response.ServerResponseResult;
import com.lanzhou.yuanfen.sys.entity.File;
import com.lanzhou.yuanfen.sys.entity.User;
import com.lanzhou.yuanfen.sys.service.IFileService;
import com.lanzhou.yuanfen.sys.service.IUserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: {@link FileController}
 * @Description: FileController
 * @author: Lanzhou
 * @date: 2020/8/16 16:39
 * @QQ: 1627518680
 * @Copyright:2020 All rights reserved.
 */
@RestController
@RequestMapping("/file/")
public class FileController {

    @Resource
    private IFileService fileService;

    @Resource
    private IUserService iUserService;

    @Resource
    private UploadProperties uploadProperties;


    /**
     * 删除文章
     *
     * @param fileKey
     * @return
     */
    @PostMapping("delImg")
    public ServerResponseResult delArchive(@RequestParam("fileKey") Long fileKey) {
        boolean save = fileService.removeById(fileKey);
        return save ? ServerResponseResult.success() : ServerResponseResult.fail();
    }

    /**
     * 添加文章
     *
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @PostMapping("pageGetImage")
    public ServerResponsePage<File> pageGetImage(@RequestParam("pageSize") Integer pageSize,
                                           @RequestParam("pageIndex") Integer pageIndex) {
        ServerResponsePage<File> responsePage = new ServerResponsePage<>();
        IPage<File> iPage = new Page<>(pageIndex, pageSize);
        IPage<File> page = fileService.page(iPage, new QueryWrapper<File>()
                .orderByDesc("create_time"));
        setCreateInfo(page.getRecords());
        responsePage.setIPage(page);
        return responsePage;
    }


    /**
     * 设置人员信息
     *
     * @param records
     */
    private void setCreateInfo(List<File> records) {
        List<User> userList = iUserService.list();
        Map<Long, String> userMap = userList.stream().collect(Collectors.toMap(User::getUserKey, User::getNickName));
        for (File record : records) {
            Long createBy = record.getCreateBy();
            String nickName = userMap.get(createBy);
            if (!StringUtils.isEmpty(nickName)) {
                record.setCreator(nickName);
            }
            record.setRemoteUrl(uploadProperties.getDownloadUrl());
        }
    }


}
